package cn.sc.allinpay.allfavour.base.connect.http;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import cn.pai.common.log.Loger;
import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.app.AppWrapper;
import cn.sc.allinpay.allfavour.business.common.app.Global;
import cn.sc.allinpay.allfavour.business.common.data.CashierData;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponData;
import cn.sc.allinpay.allfavour.business.common.data.CouponsData;
import cn.sc.allinpay.allfavour.business.common.data.DistCouponData;
import cn.sc.allinpay.allfavour.business.common.data.PosInfo;
import cn.sc.allinpay.allfavour.business.common.data.SaleCouponData;
import cn.sc.allinpay.allfavour.business.common.data.UserInfo;

/**
 * description：请求管理类
 * author：pany
 * on 2018/7/12 10:41
 */
public class RequestManager {

    /**
     * 登录
     *
     * @param account     收银员登录账号
     * @param psw         收银员密码
     * @param requestCall
     */
    public static void reqLogin(String account, String psw, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(brchkey).append("s");
        StringBuffer pswBuffer = new StringBuffer();
        pswBuffer.append(account).append(psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("employee/signIn").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("psw", MD5Tools.encrypt32MD5(pswBuffer.toString()))//密码,MD5（account +收银员密码明文）加密，取32位小写
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))//sign=(brchNo+termNo+account+key+"s")加密后的值
                .post(requestCall);
    }

    /**
     * 登录
     *
     * @param
     * @param
     * @param requestCall
     */
    public static void posLogin(String orgId, String userId, String pwd, String posCode, String terminalCode, AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }


        if (global.getServer().equals("backup")) {
            if (userId.startsWith("1") && userId.length() == 11) {
                AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/backup/user/guideUserlogin").header("Authorization", token).json().param("orgId", orgId)
                        .param("mobile", userId).param("pwd", pwd).param("posCode", posCode).param("terminalCode", terminalCode).post(requestCall);
            } else {
                AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/backup/user/guideUserlogin").header("Authorization", token).json().param("orgId", orgId)
                        .param("userId", userId).param("pwd", pwd).param("posCode", posCode).param("terminalCode", terminalCode).post(requestCall);
            }
        } else {
            if (userId.startsWith("1") && userId.length() == 11) {
                AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/user/guideUserlogin").header("Authorization", token).json().param("orgId", orgId)
                        .param("mobile", userId).param("pwd", pwd).param("posCode", posCode).param("terminalCode", terminalCode).post(requestCall);
            } else {
                AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/user/guideUserlogin").header("Authorization", token).json().param("orgId", orgId)
                        .param("userId", userId).param("pwd", pwd).param("posCode", posCode).param("terminalCode", terminalCode).post(requestCall);
            }
        }
    }

    /**
     * pos 扫码支付
     *
     * @param
     * @param
     * @param requestCall
     */

    public static void spScan(String authCode, String paymentAmount, String spOrderId, String spOrderCode, AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }


        if (global.getServer().equals("backup")) {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/backup/pos/orders/pay").header("Authorization", token).json().param("paymentMethod", "0")
                    .param("authCode", authCode).param("paymentAmount", paymentAmount).param("orderCode", spOrderCode).post(requestCall);
        } else {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/orders/pay").header("Authorization", token).json().param("paymentMethod", "0")
                    .param("authCode", authCode).param("paymentAmount", paymentAmount).param("orderId", spOrderId).post(requestCall);
        }
    }

    /**
     * pos 扫码支付结果查询
     *
     * @param
     * @param
     * @param requestCall
     */

    public static void spOrderInfo(String orderId, AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }

        if (global.getServer().equals("backup")) {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/backup/pos/orders/byOutOrderNo").header("Authorization", token).join()
                    .param("outOrderNo", orderId).get(requestCall);
        } else {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/orders/byOutOrderNo").header("Authorization", token).join()
                    .param("outOrderNo", orderId).get(requestCall);
        }

    }

    /**
     * pos 查询后台服务通道
     *
     * @param
     * @param
     * @param requestCall
     */

    public static void spServer(AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }

        AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/system/channel").header("Authorization", token).join()
                .param("t", Math.random() * 100 + "").get(requestCall);
    }

    /**
     * 修改登录密码
     *
     * @param oriPsw      原密码
     * @param newPsw      新密码
     * @param requestCall
     */
    public static void reqLoginPwdUpd(String oriPsw, String newPsw, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String tempkey1 = global.getConfigData().getTEMPKEY1();
        String tempkey2 = global.getConfigData().getTEMPKEY2();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();

        StringBuffer signBuffer = new StringBuffer();
        String oriPswB64 = Base64Tools.encrypt(tempkey1 + oriPsw + tempkey2);
        String newPswB64 = Base64Tools.encrypt(tempkey1 + newPsw + tempkey2);
        signBuffer.append(brchNo).append(termNo).append(account).append(oriPswB64).append(newPswB64).append(brchkey).append("s");//base64后的密码
        AppWrapper.getInstance().newRequest().baseUrl(url).action("employee/modifyPsw").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("oriPsw", oriPswB64)//B64加密
                .param("newPsw", newPswB64)//B64加密
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))//sign=(brchNo+termNo+account+oriPsw+newPsw+key+"s")加密后的值
                .post(requestCall);
    }

    /**
     * 会员信息查询
     *
     * @param orderAmt    订单金额
     * @param oilType     油品类型
     * @param authCode    卡号/手机号
     * @param authType    类型
     * @param couponType  券类型
     * @param requestCall
     */
    public static void reqQueryMember(String orderAmt, String oilType, String authCode, String authType, String couponType, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(authCode).append(authType)
                .append(couponType).append(orderAmt).append(oilType).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("card/info").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("authCode", authCode)//卡号/手机号
                .param("authType", authType)//类型
                .param("oilType", oilType)
                .param("orderAmt", orderAmt)
                .param("couponType", couponType)//券类型
                //sign=(brchNo+termNo+account+authCode+authType+couponType+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 加油订单列表查询
     *
     * @param payNum      订单号
     * @param requestCall
     */
    public static void reqQueryOilList(String payNum, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String storeNo = global.getConfigData().getStoreNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        if (payNum == null || TextUtils.isEmpty(payNum)) {
            payNum = "";
        }

        signBuffer.append(brchNo).append(storeNo).append(termNo).append(account).append(payNum).append(brchkey).append("s");//base64后的密码
        AppWrapper.getInstance().newRequest().baseUrl(url).action("oil/oil-order-list").json()
                .param("brchNo", brchNo)//机构号
                .param("storeNo", storeNo) // 门店号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("payNum", payNum)//订单号
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 加油订单详情查询
     *
     * @param payNum      订单号
     * @param requestCall
     */
    public static void reqQueryOilDetail(String payNum, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String storeNo = global.getConfigData().getStoreNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        if (payNum == null || TextUtils.isEmpty(payNum)) {
            payNum = "";
        }

        signBuffer.append(brchNo).append(storeNo).append(termNo).append(account).append(payNum).append(brchkey).append("s");//base64后的密码
        AppWrapper.getInstance().newRequest().baseUrl(url).action("oil/oil-order-detail").json()
                .param("brchNo", brchNo)//机构号
                .param("storeNo", storeNo) // 门店号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("payNum", payNum)//订单号
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 加油订单更新打印次数
     *
     * @param payNum      订单号
     * @param requestCall
     */
    public static void reqQueryOilUpdate(String payNum, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String storeNo = global.getConfigData().getStoreNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        if (payNum == null || TextUtils.isEmpty(payNum)) {
            payNum = "";
        }

        signBuffer.append(brchNo).append(storeNo).append(termNo).append(account).append(payNum).append(brchkey).append("s");//base64后的密码
        AppWrapper.getInstance().newRequest().baseUrl(url).action("oil/recordPrintReceipt").json()
                .param("brchNo", brchNo)//机构号
                .param("storeNo", storeNo) // 门店号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("payNum", payNum)//订单号
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 订单支付结果通知
     *
     * @param
     * @param
     */
    public static void payNotify(String order_id, String trans_way, String traxstatus, String trans_time, String c_amount, String a_amount, String transactionNumber, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String storeNo = global.getConfigData().getStoreNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        if (c_amount == null || TextUtils.isEmpty(c_amount)) {
            c_amount = "0";
        }

        if (a_amount == null || TextUtils.isEmpty(a_amount)) {
            a_amount = "0";
        }

        signBuffer.append(brchNo).append(storeNo).append(termNo).append(account).append(order_id).append(trans_way).append(traxstatus).append(trans_time).append(c_amount).append(a_amount).append(brchkey).append("s");//base64后的密码
        AppWrapper.getInstance().newRequest().baseUrl(url).action("order/paycore/notify").json()
                .param("brchNo", brchNo)//机构号
                .param("storeNo", storeNo) // 门店号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("order_id", order_id)//订单号
                .param("trans_way", trans_way)
                .param("traxstatus", traxstatus)
                .param("trans_time", trans_time)
                .param("c_amount", c_amount)
                .param("a_amount", a_amount)
                .param("sybTransNo", transactionNumber)
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);
    }

    /**
     * 商点订单创建
     *
     * @param
     * @param
     * @param requestCall
     */
    public static void spCreateOrder(String amount, String outOrderNo, AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }
        UserInfo userInfo = global.getUserInfo();
        PosInfo posInfo = global.getPosInfo();


        if (global.getServer().equals("backup")) {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/backup/pos/orders").header("Authorization", token).json().param("amount", amount)
                    .param("shopId", userInfo.getShopId()).param("termNo", posInfo.getTer_id()).param("mchtCd", posInfo.getMer_id()).param("outOrderNo", outOrderNo).post(requestCall);
        } else {
            AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/orders").header("Authorization", token).json().param("amount", amount)
                    .param("shopId", userInfo.getShopId()).param("termNo", posInfo.getTer_id()).param("mchtCd", posInfo.getMer_id()).param("outOrderNo", outOrderNo).post(requestCall);
        }
    }

    /**
     * 商点订单撤销
     *
     * @param
     * @param
     * @param requestCall
     */
    public static void spCancelOrder(String orderCode, AppRequest.PosRequestCall requestCall) {
        Global global = Global.getInstance();
        String token = "";
        if (global.getToken() != null) {
            token = global.getToken().getToken();
        }
        UserInfo userInfo = global.getUserInfo();
        PosInfo posInfo = global.getPosInfo();
        AppWrapper.getInstance().newRequest().baseUrl("https://dms-api.shopoint.cn/shopoint-pos-web").action("/orders/trans/cancel").header("Authorization", token).form().param("orderCode", orderCode).post(requestCall);
    }


    /**
     * 会员核销订单创建
     * 包括：优惠，卡券，电子钱包
     *
     * @param authCode    会员动态码 卡号
     * @param totAmt      订单总金额
     * @param txnAmt      消费金额
     * @param coupons     使用券
     * @param couponAmt   用券总金额
     * @param rightsAmt   会员权益优惠金额
     * @param actulAmt    实付金额
     * @param oilType     油品号
     * @param requestCall
     */
    public static void reqSellOrder(String authCode, String totAmt, String txnAmt, List<CouponsData> coupons,
                                    String couponAmt, String rightsAmt, String actulAmt, String oilType, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        String couponsSign = "";
        JSONArray couponsArray = null;
        if (!TextUtils.isEmpty(couponAmt) && coupons != null && !coupons.isEmpty()) {//卡券折扣金额不为空
            StringBuffer couponSignBuffer = new StringBuffer();
            couponsArray = new JSONArray();
            for (int i = 0; i < coupons.size(); i++) {
                CouponsData couponsData = coupons.get(i);
                if (couponsData.isUse()) {
                    if (couponsData.getUseNumber() == 0 && (couponsData.getUseAmt() == null || TextUtils.isEmpty(couponsData.getUseAmt()) || BigDecimal.ZERO.compareTo(new BigDecimal(couponsData.getUseAmt())) == 0)) {
                    } else {
                        couponSignBuffer.append(couponsData.getCouponCode());
                        JSONObject couponObject = new JSONObject();
                        couponObject.put("couponNum", couponsData.getCouponCode());
                        if (couponsData.getType() == 0) {//金额券
                            couponObject.put("num", AmtUtils.changeY2F(couponsData.getUseAmt()));
                            couponSignBuffer.append(AmtUtils.changeY2F(couponsData.getUseAmt()));
                        } else {
                            couponObject.put("num", couponsData.getUseNumber());
                            couponSignBuffer.append(couponsData.getUseNumber());
                        }
                        couponsArray.add(couponObject);
                    }

                }
            }
            couponsSign = couponSignBuffer.toString();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -5);//向前五分钟与服务器同步时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String reqTime = sdf.format(calendar.getTime());

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(!TextUtils.isEmpty(authCode) ? authCode: "").append(AmtUtils.changeY2F(totAmt)).
                append(!TextUtils.isEmpty(txnAmt) ? AmtUtils.changeY2F(txnAmt) : "").append(couponsSign).
                append(!TextUtils.isEmpty(couponAmt) ? AmtUtils.changeY2F(couponAmt) : "").
                append(!TextUtils.isEmpty(rightsAmt) ? AmtUtils.changeY2F(rightsAmt) : "").
                append(AmtUtils.changeY2F(actulAmt)).append(reqTime).append(oilType).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("order/txn").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("authCode", authCode)//卡号/手机号
                .param("totAmt", AmtUtils.changeY2F(totAmt))//总金额
                .param("txnAmt", AmtUtils.changeY2F(txnAmt))//消费金额，电子钱包金额
                .param("coupons", couponsArray)//卡券信息
                .param("couponAmt", AmtUtils.changeY2F(couponAmt))//卡券总金额
                .param("rightsAmt", AmtUtils.changeY2F(rightsAmt))//会员优惠金额
                .param("actulAmt", AmtUtils.changeY2F(actulAmt))//实付金额
                .param("reqTime", reqTime)//时间戳
                .param("oilType", oilType)
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);
    }

    /**
     * 积分核销
     *
     * @param requestCall
     */
    public static void reqVoidIntegral(String payNum, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(payNum).append(brchkey).append("s");//base64后的密码
        Loger.d(signBuffer.toString());

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("order/cancelSendCouponPoint").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("payNum", payNum)//支付订单号
                //sign=(brchNo+termNo+account+payNum+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 会员卡订单撤销接口(订单中的用券和用卡余额)
     *
     * @param payNum      支付订单号
     * @param requestCall
     */
    public static void reqVoidOrder(String payNum, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(payNum).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("order/cancelAmtCoupon").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("payNum", payNum)//卡号/手机号
                //sign=(brchNo+termNo+account+payNum+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 发券列表查询接口
     *
     * @param requestCall
     */
    public static void reqQueryDistCoupon(AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/sendList").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                //sign=(brchNo+termNo+account+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 发券订单接口
     *
     * @param requestCall
     */
    public static void reqDistCoupon(List<DistCouponData> distDatas, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        String distSign = "";
        JSONArray distArray = null;
        if (distDatas != null && !distDatas.isEmpty()) {//卡券折扣金额不为空
            StringBuffer distSignBuffer = new StringBuffer();
            distArray = new JSONArray();
            for (int i = 0; i < distDatas.size(); i++) {
                DistCouponData distData = distDatas.get(i);
                distSignBuffer.append(distData.getId());
                distSignBuffer.append(distData.getUseNum());
                JSONObject distObject = new JSONObject();
                distObject.put("id", distData.getId());
                distObject.put("num", String.valueOf(distData.getUseNum()));
                distArray.add(distObject);
            }
            distSign = distSignBuffer.toString();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -5);//向前五分钟与服务器同步时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String reqTime = sdf.format(calendar.getTime());

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(distSign).
                append(reqTime).append(brchkey).append("s");//base64后的密码
        Loger.d(signBuffer.toString());

        String newPsw = MD5Tools.encrypt32MD5(account + psw);

        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/send").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("coupons", distArray)//发券参数
                .param("reqTime", reqTime)//时间
                //sign=(brchNo+termNo+account+id1+num1+id2+num2+.....+reqTime +key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 售券列表查询接口
     *
     * @param requestCall
     */
    public static void reqQrySaleCoupon(AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);
        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/saleList").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                //sign=(brchNo+termNo+account+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 售券订单接口
     *
     * @param orderNo     订单号
     * @param txnAmt      支付金额
     * @param txnType     字符类型
     * @param saleDatas   售券列表
     * @param requestCall
     */
    public static void reqSaleCoupon(String orderNo, String txnAmt, String txnType, List<SaleCouponData> saleDatas, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        String saleSign = "";
        JSONArray saleArray = null;
        if (saleDatas != null && !saleDatas.isEmpty()) {//卡券折扣金额不为空
            StringBuffer saleSignBuffer = new StringBuffer();
            saleArray = new JSONArray();
            for (int i = 0; i < saleDatas.size(); i++) {
                SaleCouponData saleData = saleDatas.get(i);
                saleSignBuffer.append(saleData.getId());
                saleSignBuffer.append(saleData.getUseNum());
                JSONObject saleObject = new JSONObject();
                saleObject.put("id", saleData.getId());
                saleObject.put("num", String.valueOf(saleData.getUseNum()));
                saleArray.add(saleObject);
            }
            saleSign = saleSignBuffer.toString();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -5);//向前五分钟与服务器同步时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String reqTime = sdf.format(calendar.getTime());

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(saleSign).append(txnAmt).
                append(txnType).append(orderNo).append(reqTime).append(brchkey).append("s");//base64后的密码
        Loger.d(signBuffer.toString());

        String newPsw = MD5Tools.encrypt32MD5(account + psw);

        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/sale").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("coupons", saleArray)//发券参数
                .param("txnAmt", txnAmt)//交易金额
                .param("txnType", txnType)//交易类型
                .param("orderNo", orderNo)//订单号
                .param("reqTime", reqTime)//时间
                //sign=(brchNo+termNo+account+id1+num1+id2+num2+.....+reqTime +key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }

    /**
     * 验券接口
     *
     * @param requestCall
     */
    public static void reqCheckCoupon(List<CheckCouponData> coupons, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        String couponSign = "";
        JSONArray couponArray = null;
        if (coupons != null && !coupons.isEmpty()) {
            StringBuffer couponSignBuffer = new StringBuffer();
            couponArray = new JSONArray();
            for (int i = 0; i < coupons.size(); i++) {
                CheckCouponData couponData = coupons.get(i);
                if (couponData.isUse()) {
                    couponSignBuffer.append(couponData.getCouponNo());
                    JSONObject couponObject = new JSONObject();
                    couponObject.put("couponNo", couponData.getCouponNo());
                    couponObject.put("remark", couponData.getRemark());
                    if (couponData.getType() == 0) {//金额券
                        couponObject.put("num", AmtUtils.changeY2F(couponData.getUseAmt()));
                        couponSignBuffer.append(AmtUtils.changeY2F(couponData.getUseAmt()));
                    } else {
                        couponObject.put("num", couponData.getUseNumber());
                        couponSignBuffer.append(couponData.getUseNumber());
                    }
                    couponSignBuffer.append(couponData.getRemark());
                    couponArray.add(couponObject);
                }
            }
            couponSign = couponSignBuffer.toString();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -5);//向前五分钟与服务器同步时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String reqTime = sdf.format(calendar.getTime());

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(couponSign).
                append(reqTime).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);

        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/chk").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("chkCoupons", couponArray)//发券参数
                .param("reqTime", reqTime)//时间
                //sign=(brchNo+termNo+account+id1+num1+id2+num2+.....+reqTime +key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }


    /**
     * 卡券信息查询   根据券号查询
     *
     * @param couponNo    券号
     * @param requestCall
     */
    public static void reqQueryCouponByNo(String couponNo, AppRequest.RequestCall requestCall) {
        Global global = Global.getInstance();
        String url = global.getConfigData().getBASEURL();
        String brchkey = global.getConfigData().getBRCHKEY();
        String brchNo = global.getConfigData().getBrchNo();
        String termNo = global.getConfigData().getTerId();
        CashierData cashierData = global.getCashierData();
        String account = cashierData.getCashierNo();
        String psw = cashierData.getPassword();

        StringBuffer signBuffer = new StringBuffer();
        signBuffer.append(brchNo).append(termNo).append(account).append(couponNo).append(brchkey).append("s");//base64后的密码

        String newPsw = MD5Tools.encrypt32MD5(account + psw);

        AppWrapper.getInstance().newRequest().baseUrl(url).action("coupon/preChk").json()
                .param("brchNo", brchNo)//机构号
                .param("termNo", termNo)//终端号
                .param("account", account)//收银员登录账号
                .param("couponNo", couponNo)//券号
                //sign=(brchNo+termNo+account+couponNo+key+"s")加密后的值
                .param("sign", MD5Tools.encrypt32MD5(signBuffer.toString()))
                .param("psw", newPsw)//收银员密码
                .post(requestCall);

    }
}
